﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HRM.Model;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using HRM.View;
using GalaSoft.MvvmLight.Ioc;
using System.Windows.Forms;
using System.IO;

namespace HRM.ViewModel
{
    public class StaffViewModel : ViewModelGeneric<NhanVien>
    {
        private IEntityService<TrinhDoHocVan> _elService;

        public StaffViewModel(IEntityService<NhanVien> service, IEntityService<TrinhDoHocVan> elService)
            : base(service)
        {
            _elService = elService;
            EducationLevels = _elService.GetAll();
            SelectedItem.GioiTinh = true;
        }

        private List<TrinhDoHocVan> _els;

        public List<TrinhDoHocVan> EducationLevels
        {
            get { return _els; }
            set
            {
                if (_els != value)
                {
                    _els = value;
                    RaisePropertyChanged("EducationLevels");
                }
            }
        }

        private TrinhDoHocVan _elSelected;

        public TrinhDoHocVan ELSelected
        {
            get
            {
                return _elSelected;
            }
            set
            {
                if (_elSelected != value)
                {
                    _elSelected = value;
                    RaisePropertyChanged("ELSelected");
                }
            }
        }

        public override NhanVien SelectedItem
        {
            get
            {
                return base.SelectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    if (_selectedItem != null)
                    {
                        if (!UpdateMode) UpdateMode = true;
                        if (SelectedItem.TrinhDoHocVan != null)
                        {
                            ELSelected = EducationLevels.Find(x => x.MaTrinhDo == SelectedItem.TrinhDoHocVan.MaTrinhDo);
                        }
                        else
                        {
                            ELSelected = EducationLevels[0];
                        }
                        Messenger.Default.Send(new StatusMessage("Click chuột phải để xem chi tiết", 2000, StatusType.Info));
                    }
                    RaisePropertyChanged("SelectedItem");
                }
            }
        }

        public override RelayCommand SaveCommand
        {
            get
            {
                return new RelayCommand(() =>
                    {
                        if (SelectedItem == null) return;

                        if (ELSelected == null)
                        {
                            Messenger.Default.Send(new StatusMessage("Chưa chọn Trình độ học vấn", 3000, StatusType.Warning));
                            return;
                        }

                        if (SelectedItem.HoTen == null)
                        {
                            Messenger.Default.Send(new StatusMessage("Chưa nhập tên nhân viên", 3000, StatusType.Warning));
                            return;
                        }

                        if (SelectedItem.NgaySinh != null)
                        {
                            if (DateTime.Compare(Convert.ToDateTime(SelectedItem.NgaySinh), new DateTime(1900, 1, 1)) < 0 || DateTime.Compare(Convert.ToDateTime(SelectedItem.NgaySinh), DateTime.Now) > 0)
                            {
                                Messenger.Default.Send(new StatusMessage(string.Format("Ngày sinh không hợp lệ (1/1/1900 - {0}", DateTime.Now.ToString("dd/mm/yyyy")), 3000, StatusType.Warning));
                                return;
                            }
                        }
                        else
                        {
                            Messenger.Default.Send(new StatusMessage("Chưa nhập ngày sinh", 3000, StatusType.Warning));
                            return;
                        }


                        if (AddMode)
                        {
                            SelectedItem.KinhNghiems = null;
                            SelectedItem.BangLuongs = null;
                            SelectedItem.ChungChis = null;
                            SelectedItem.ThoiViecs = null;
                            SelectedItem.ThamGiaDuAns = null;
                            SelectedItem.QuaTrinhCongTacs = null;
                            SelectedItem.NguoiSuDung = null;
                            SelectedItem.TrinhDoHocVan = null;
                            SelectedItem.MaTDHV = ELSelected.MaTrinhDo;
                            if (_service.Add(SelectedItem)) { }
                            else
                            {
                                Messenger.Default.Send(new StatusMessage("Lỗi truy cập Database", 3000, StatusType.Error));
                            }
                        }
                        else if (UpdateMode)
                        {
                            _service.Update(SelectedItem);
                        }
                        Refresh();
                    });
            }
        }

        public RelayCommand WorkingHistoryDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                    {
                        SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                        WorkingHistoryDialog dialog = new WorkingHistoryDialog();
                        dialog.ShowDialog();
                    }, () =>
                    {
                        if (SelectedItem != null) return true;
                        else return false;
                    });
            }
        }

        public RelayCommand CertificateDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    CertificateDialog dialog = new CertificateDialog();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand SalaryDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    SalaryDialog dialog = new SalaryDialog();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand ResignDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    ResignDialog dialog = new ResignDialog();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand PPDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    PParticipantDiolog dialog = new PParticipantDiolog();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand ExDetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    ExperienceDialog dialog = new ExperienceDialog();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand DetailCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    SimpleIoc.Default.Register<NhanVien>(() => SelectedItem);
                    StaffDetailWindow dialog = new StaffDetailWindow();
                    dialog.ShowDialog();
                }, () =>
                {
                    if (SelectedItem != null) return true;
                    else return false;
                });
            }
        }

        public RelayCommand ChangeImageCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.DefaultExt = ".jpg";
                    dlg.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
                    dlg.Title = "Chọn ảnh đại diện.";
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        string filePath = dlg.FileName;
                        string fileName = Path.GetFileName(filePath);
                        string folderDes = Properties.Settings.Default.FolderSourcePath;
                        System.IO.File.Copy(filePath, string.Format("{0}\\{1}", folderDes, fileName), true);
                        SelectedItem.AnhDaiDien = Properties.Settings.Default.LocalFolder + "/" + fileName;
                    }
                });
            }
        }
    }
}
